{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2 as cv\n",
    "from calibrator import *\n",
    "from glob import glob\n",
    "from os.path import join\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "DIR = \"D:/CarImg\"\n",
    "EXT = \".png\"\n",
    "BOARD = (8, 6)\n",
    "calibrator = MonoCalibrator([ChessboardInfo(*BOARD)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "if 0:\n",
    "    dirs = glob(join(DIR, \"*\" + EXT))\n",
    "    for d in dirs:\n",
    "        img = cv.imread(d, cv.IMREAD_GRAYSCALE)\n",
    "        ret, _, _ = calibrator.get_corners(img)\n",
    "        if not ret:\n",
    "            print(d)\n",
    "            os.remove(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "dirs = glob(join(DIR, \"*\" + EXT))\n",
    "imgs = [cv.imread(d, cv.IMREAD_GRAYSCALE) for d in dirs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mono pinhole calibration...\n",
      "[[-4.32345843e-01]\n",
      " [ 2.42164454e-01]\n",
      " [ 2.86863601e-04]\n",
      " [ 5.35992043e-04]\n",
      " [-8.04626098e-02]]\n",
      "[[687.22577179   0.         376.04268025]\n",
      " [  0.         687.2976733  242.83241295]\n",
      " [  0.           0.           1.        ]]\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[[589.70233154   0.         376.15877696   0.        ]\n",
      " [  0.         648.32983398 242.75541548   0.        ]\n",
      " [  0.           0.           1.           0.        ]]\n"
     ]
    }
   ],
   "source": [
    "calibrator.cal(imgs)\n",
    "calibrator.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def FloatArray2Str(a:np.ndarray) -> str:\n",
    "    a = a.flatten()\n",
    "    return \"0x\" + \",0x\".join(a.tobytes().hex(',', 4).split(',')) + \"\"\n",
    "\n",
    "def FloatArrayRound2Str(a:np.ndarray) -> str:\n",
    "    a = np.round_(a.flatten()).astype(np.int16)\n",
    "    return \",\".join(map(str,a))\n",
    "\n",
    "with open(\"mapx.txt\", \"w\") as f:\n",
    "    f.write(FloatArray2Str(calibrator.mapx))\n",
    "\n",
    "with open(\"mapy.txt\", \"w\") as f:\n",
    "    f.write(FloatArray2Str(calibrator.mapy))\n",
    "\n",
    "with open(\"mapxI.txt\", \"w\") as f:\n",
    "    f.write(FloatArrayRound2Str(calibrator.mapx))\n",
    "\n",
    "with open(\"mapyI.txt\", \"w\") as f:\n",
    "    f.write(FloatArrayRound2Str(calibrator.mapy))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "for img in imgs:\n",
    "    rect = calibrator.remap(img)\n",
    "    cv.imshow(\"img\", np.hstack((img, rect)))\n",
    "    if cv.waitKey(0) == 27:  # esc\n",
    "        break\n",
    "cv.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "63edd14921664e8e4a358dc9242e0c95518894f8813319b693520631dd4bb74f"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('FBScope')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
